// pages/paimaiing/paimaiing.js
var common = require("../../public");
var url = common.url;
var APP_ID = common.APP_ID;
var md5 = common.md5Start;
Page({

  /**
   * 页面的初始数据
   */
  data: {
	  orderlist: [
		  { goods_name: 'sdkfskdf', goods_price: '899', old_price: '66' },
		//   { goods_name: 'sdkfskdf', goods_price: '899', old_price: '66' },
		//   { goods_name: 'sdkfskdf', goods_price: '899', old_price:'66'},
	  ],
	},
	//跳转到购物战场
	go_warfield:function(e){
		var id = e.currentTarget.dataset.id;//购物战场id
		wx.navigateTo({
			url: '../gouwuzhanchang/gouwuzhanchang?id='+id,
		})
	},
	// 倒计时（热门推荐)
	setCountDown_hot: function () {
		let timer_hot = this.data.timer_hot;
		if (timer_hot) {
			clearTimeout(timer_hot);
		}
		let time = 1000;
		let {
			recomment_list
		} = this.data;
		let list = recomment_list.map((v, i) => {
			if (v.is_start == 1 && v.is_end == 0) {
				//已开始  正计时
				if (v.timestamp) {
					if (v.timestamp <= 0) {
						v.timestamp = 0;
						v.countDown = '00:00:00';
						// return v;
					}
					let formatTime = this.getFormat(v.timestamp);
					v.timestamp += time;
					v.countDown = `${formatTime.hh}:${formatTime.mm}:${formatTime.ss}`;

				} else {
					v.countDown = '00:00:00'; //============做判断   如果没有倒计时的话
				}
			}
			if (v.is_start == 2 && v.is_end == 0) {
				//未开始  倒计时
				if (v.timestamp) {
					if (v.timestamp <= 0) {
						v.timestamp = 0;
						v.countDown = '00:00:00';
						// return v;
					}
					let formatTime = this.getFormat(v.timestamp);
					v.timestamp -= time;
					v.countDown = `${formatTime.hh}:${formatTime.mm}:${formatTime.ss}`;

				} else {
					v.countDown = '00:00:00'; //============做判断   如果没有倒计时的话
				}
			}
			return v;

		})
		this.setData({
			recomment_list: list
		});
		timer_hot = setTimeout(this.setCountDown_hot, time);
		this.setData({
			timer_hot: timer_hot
		})
	},

    /**
     * 格式化时间
     */
	getFormat: function (msec) {
		let ss = parseInt(msec / 1000);
		let ms = parseInt(msec % 1000);
		let mm = 0;
		let hh = 0;
		if (ss > 60) {
			mm = parseInt(ss / 60);
			ss = parseInt(ss % 60);
			if (mm > 60) {
				hh = parseInt(mm / 60);
				mm = parseInt(mm % 60);
			}
		}
		ss = ss > 9 ? ss : `0${ss}`;
		mm = mm > 9 ? mm : `0${mm}`;
		hh = hh > 9 ? hh : `0${hh}`;
		return {
			ms,
			ss,
			mm,
			hh
		};
	}, 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	  var that = this;
	  this.setData({
		  page:1
	  })
	  var session_str = wx.getStorageSync('session_str');//获取密钥
	  if (!session_str){
		  wx.navigateTo({
			  url: '../author/author',
		  })
		  return false;
	  }
	  wx.showLoading({
		  title: '加载中',
		  mask: true,
	  })
	  //请求拍卖中列表数据
	  wx.request({
		  url: url + 'Order/auctionList',
		  data: {
			  session_str: session_str,
			  page: 1,
			  APP_ID: APP_ID,
			  sign: md5([
				  "session_str" + session_str,
				  "page1",
				  "APP_ID" + APP_ID,
			  ])
		  },
		  header: {},
		  method: 'post',
		  dataType: 'json',
		  responseType: 'text',
		  success: function (res) {
			  wx.hideLoading();
			  if (res.data.code == 200) {
				  var resdata = res.data.data;
				  var now_timstamp = Date.parse(new Date());
				  for (let i = 0; i < resdata.length; i++) {
					  if (resdata[i].start_time * 1000 > now_timstamp) {
						  resdata[i].timestamp = resdata[i].start_time * 1000 - now_timstamp;
					  } else {
						  resdata[i].timestamp = now_timstamp - resdata[i].start_time * 1000;
					  }
				  }
				  
				  that.setData({
					  recomment_list: res.data.data, //订单数据
				  })
				  that.setCountDown_hot();
				  if (res.data.data.length == 0) {
					  that.setData({
						  showNoneData: true
					  })
				  }
			  } else {
				  wx.showToast({
					  title: res.data.msg,
					  icon: 'none',
					  mask: true,
				  })
			  }
		  },
		  fail: function (res) {
			  wx.hideLoading();
			  wx.showToast({
				  title: '网络错误',
				  icon: 'none',
				  mask: true,
			  })
		  }
	  });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
	  var that = this;
	  var page = this.data.page;//页码
	  var orderlist = this.data.orderlist;//数据列表
	  var session_str = wx.getStorageSync('session_str');//获取密钥
	  if (!session_str) {
		  wx.navigateTo({
			  url: '../author/author',
		  })
		  return false;
	  }
	  wx.showLoading({
		  title: '加载中',
		  mask: true,
	  })
	  page++;
	  //请求拍卖中列表数据
	  wx.request({
		  url: url + 'Order/auctionList',
		  data: {
			  session_str: session_str,
			  page: page,
			  APP_ID: APP_ID,
			  sign: md5([
				  "session_str" + session_str,
				  "page" + page,
				  "APP_ID" + APP_ID,
			  ])
		  },
		  header: {},
		  method: 'post',
		  dataType: 'json',
		  responseType: 'text',
		  success: function (res) {
			  wx.hideLoading();
			  if (res.data.code == 200) {
				  var resdata = res.data.data;
				  if (resdata.length == 0) {
					  wx.showToast({
						  title: '没有更多数据',
						  icon: 'none',
						  mask: true,
					  })
				  }
				  var now_timstamp = Date.parse(new Date());
				  for (let i = 0; i < resdata.length; i++) {
					  if (resdata[i].start_time * 1000 > now_timstamp) {
						  resdata[i].timestamp = resdata[i].start_time * 1000 - now_timstamp;
					  } else {
						  resdata[i].timestamp = now_timstamp - resdata[i].start_time * 1000;
					  }
				  }
				  recomment_list = recomment_list.concat(resdata)
				  that.setData({
					  recomment_list: recomment_list,
					  page: page
				  })
				  that.setCountDown_hot();
				  
			  } else {
				  wx.showToast({
					  title: res.data.msg,
					  icon: 'none',
					  mask: true,
				  })
			  }
		  },
		  fail: function (res) {
			  wx.hideLoading();
			  wx.showToast({
				  title: '网络错误',
				  icon: 'none',
				  mask: true,
			  })
		  }
	  });
  },
})